package com.leisurexi.aop.features;

import com.leisurexi.aop.features.interceptor.EchoServiceMethodInterceptor;
import com.leisurexi.aop.features.pointcut.EchoServiceEchoMethodPointcut;
import com.leisurexi.aop.features.pointcut.EchoServicePointcut;
import com.leisurexi.aop.overview.DefaultEchoService;
import com.leisurexi.aop.overview.EchoService;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;

/**
 * @author: leisurexi
 * @date: 2021-05-23 20:21
 */
public class PointcutAPIDemo {

    public static void main(String[] args) {
        EchoServicePointcut echoServicePointcut = new EchoServicePointcut("echo", EchoService.class);
//        EchoServiceEchoMethodPointcut pointcut = new EchoServiceEchoMethodPointcut();

        // 组合实现
        ComposablePointcut pointcut = new ComposablePointcut(new EchoServiceEchoMethodPointcut());
        pointcut.intersection(echoServicePointcut.getClassFilter());
        pointcut.intersection(echoServicePointcut.getMethodMatcher());

        // 将 pointcut 设配成 Advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new EchoServiceMethodInterceptor());
        DefaultEchoService echoService = new DefaultEchoService();
        ProxyFactory proxyFactory = new ProxyFactory(echoService);
        // 添加 Advisor
        proxyFactory.addAdvisor(advisor);
        // 获取代理对象
        EchoService proxyEchoService = (EchoService) proxyFactory.getProxy();
        System.out.println(proxyEchoService.echo("Hello, World!"));
    }

}
